package com.qdlc.p2p.manage.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.TppDifferenceService;
import com.qdlc.p2p.biz.util.excel.ExcelUtils;
import com.qdlc.p2p.biz.util.excel.JsGridReportBase;
import com.qdlc.p2p.biz.util.excel.TableData;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.util.MessageUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dao.TppDifferenceDao;
import com.qdlc.p2p.dal.dto.TppDifference;
import com.qdlc.p2p.dal.model.TppDifferenceModel;

/**
 * 用户托管资金差额管理
 * 
 * @author ygw
 * @version 2.0
 * @since 2015-11-22
 */
public class ManageTppDifferenceAction extends BaseAction<TppDifferenceModel> implements
		ModelDriven<TppDifferenceModel> {

	@Resource
	private TppDifferenceService tppDifferenceService;
	
	@Resource
	private TppDifferenceDao tppDifferenceDao;

	private Map<String, Object> map = new HashMap<String, Object>();

	/**
	 * 用户对账（托管资金差额）页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action("/modules/account/account/tppDifference")
	public String tppDifference() throws Exception {
		return "tppDifference";
	}

	/**
	 * 用户托管资金差额列表
	 * 
	 * @throws Exception
	 */
	@Action("/modules/account/account/tppDifferenceList")
	public void tppDifferenceList() throws Exception {
		if(StringUtil.isBlank(paramString("status"))){
			model.setStatus(1);
		}
		PageDataList<TppDifferenceModel> pageList = tppDifferenceService.list(model);
		int totalPage = pageList.getPage().getTotal();
		map.put("total", totalPage);
		map.put("rows", pageList.getList());
		printJson(getStringOfJpaObj(map));
	}

	/**
	 * 处理资金对账页面
	 * 
	 * @throws IOException
	 */
	@Action("/modules/account/account/setTppDifference")
	public String setTppDifference() throws Exception {
		
		TppDifference tppDifference = tppDifferenceDao.find(model.getId());
		request.setAttribute("tppDifference", tppDifference);
		saveToken("doSetTppDifferenceToken");
		return "setTppDifference";
	}
	
	/**
	 * 处理资金对账
	 * 
	 * @throws IOException
	 */
	@Action("/modules/account/account/doSetTppDifference")
	public void doSetTppDifference() throws Exception {
		
		checkToken("doSetTppDifferenceToken");
		model.setUserId(paramLong("userId"));
		tppDifferenceService.doSetTppDifference(model, getOperator());
		printResult(MessageUtil.getMessage("MF105001011"), true);
	}

	/**
	 * 用户托管资金差额导出报表
	 * 
	 * @throws Exception
	 */
	@Action("/modules/account/account/exportExcelDifference")
	public void exportExcelDifference() throws Exception {
		response.setContentType("application/msexcel;charset=UTF-8");
		
		// 第一页开始
		model.setPage(1);
		// 最多出5000条记录
		model.setRows(5000);
		model.setSort("id");
		model.setOrder("asc");
		// 记录取得
		List<TppDifferenceModel> list = tppDifferenceService.list(model).getList();
		String title = "资金对账Excel表";
		
		String[] hearders = new String[] { "编号", "用户名", "真实姓名",
				"可用金额(元)","托管可用金额(元)","可用差额(元)", "托管ID","添加时间","更新时间","备注" };// 表头数组
		String[] fields = new String[] { "id", "username", "realname", "useMoneyString",
				"tppUseMoneyString","differenceUseString", "tppUserCustId","addTime","updateTime","remark" };// 对象属性数组
		TableData td = ExcelUtils.createTableData(list,
				ExcelUtils.createTableHeader(hearders), fields);
		JsGridReportBase report = new JsGridReportBase(request, response);
		report.exportToExcel(title, getOperator().getUserName(), td);
	}

}
